import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Felix
 * @date 2024/1/22
 */
public class RedisDemo {
    private static JedisPool jedisPool ;

    static {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMinIdle(5);
        poolConfig.setMaxTotal(100);
        poolConfig.setMaxIdle(5);
        poolConfig.setBlockWhenExhausted(true);
        poolConfig.setMaxWaitMillis(2000);
        poolConfig.setTestOnBorrow(true);
        jedisPool = new JedisPool(poolConfig,"hadoop102",6379,10000);
    }

    //获取操作Redis的Java客户端Jedis
    public static Jedis getJedis(){
        //方式1：通过new直接创建Jedis对象
        //Jedis jedis = new Jedis("hadoop102",6379);
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }

    public static void main(String[] args) {
        Jedis jedis = getJedis();
        String pong = jedis.ping();
        jedis.set("k11","v11");
        System.out.println(pong);
        jedis.close();
    }
}
